class Solution
{
public:
    int countDaysTogether(string arriveAlice, string leaveAlice, string arriveBob, string leaveBob)
    {
        int arriveAliceDay = dayOfYear(arriveAlice);
        int leaveAliceDay = dayOfYear(leaveAlice);
        int arriveBobDay = dayOfYear(arriveBob);
        int leaveBobDay = dayOfYear(leaveBob);
        if (arriveAliceDay > leaveBobDay || arriveBobDay > leaveAliceDay)
        {
            return 0;
        }
        if (arriveAliceDay >= arriveBobDay && leaveAliceDay <= leaveBobDay)
        {
            return leaveAliceDay - arriveAliceDay + 1;
        }
        if (arriveBobDay >= arriveAliceDay && leaveBobDay <= leaveAliceDay)
        {
            return leaveBobDay - arriveBobDay + 1;
        }
        if (arriveBobDay <= leaveAliceDay && arriveBobDay >= arriveAliceDay)
        {
            return leaveAliceDay - arriveBobDay + 1;
        }
        return leaveBobDay - arriveAliceDay + 1;
    }

    int dayOfYear(string &date)
    {
        const vector<int> monthDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int month = stoi(date.substr(0, 2));
        int day = stoi(date.substr(3, 2));
        int result = 0;
        for (int i = 0; i < month - 1; ++i)
        {
            result += monthDays[i];
        }
        result += day;
        return result;
    }
};